package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.exam.ExamResultItemBean;

import java.util.List;

/**
 * @date on 2018/8/19 2:30
 * @email 909732184@qq.com
 * 展示考试结果答案对错一览数据
 */
public class ExamResultAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private Drawable[] drawables = new Drawable[3];
    private int whiteColor,blackColor;
    private OnViewItemClickListener onViewItemClickListener;
    private List<ExamResultItemBean> data;

    public OnViewItemClickListener getOnViewItemClickListener() {
        return onViewItemClickListener;
    }

    public void setOnViewItemClickListener(OnViewItemClickListener onViewItemClickListener) {
        this.onViewItemClickListener = onViewItemClickListener;
    }

    public List<ExamResultItemBean> getData() {
        return data;
    }

    public void setData(List<ExamResultItemBean> data) {
        this.data = data;
    }

    public interface OnViewItemClickListener{
        void onCurriculumItemClick(int position);
    }

    public ExamResultAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        drawables[0] = ContextCompat.getDrawable(context,R.drawable.cutting_exam_result_blue);
        drawables[1] = ContextCompat.getDrawable(context,R.drawable.cutting_exam_result_red);
        drawables[2] = ContextCompat.getDrawable(context,R.drawable.cutting_exam_result_white);
        whiteColor = ContextCompat.getColor(context,R.color.white);
        blackColor = ContextCompat.getColor(context,R.color.title);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.exam_result_grid_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.position = position;
        viewHolder.tvText.setText((position+1)+"");
        String isCorrect = data.get(position).getIsCorrect();
        if(isCorrect==null) {
            viewHolder.tvText.setBackground(drawables[2]);
            viewHolder.tvText.setTextColor(blackColor);
        }else {
            if(isCorrect.equals("1")){
                viewHolder.tvText.setBackground(drawables[0]);
                viewHolder.tvText.setTextColor(whiteColor);
            }else {
                viewHolder.tvText.setBackground(drawables[1]);
                viewHolder.tvText.setTextColor(whiteColor);
            }
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private int position;
        private TextView tvText;
        public ViewHolder(View itemView) {
            super(itemView);
            tvText = itemView.findViewById(R.id.tvText);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onViewItemClickListener!=null){
                        onViewItemClickListener.onCurriculumItemClick(position);
                    }
                }
            });

        }
    }
}
